/**
 * Created by jt on 2017/5/8.
 */
import "./index.less";

import ProjOnResultsListTpl from "./proj_on_results_list.handlebars";
import paginationTpl from "./paging.handlebars";

//最新资讯高亮
jQuery('.navPart ul li').eq(1).addClass('active');

var ProjOnResultsList = {
    pageNo: 1,
    pageSize: 20,
    maxPage: 1,
    isFirstPage: false,
    isLastPage: false,
    total: 0,
    init: function(){
        this.getProjOnResultsData()
            .then(this.renderProjOnResultsList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    },
    getProjOnResultsData: function() {
        return new Promise((resolve, reject)=>{
            let self = this;
            $.ajax({
                url: HOST_ADDR + `/cpmp/xjProjMngForPortal/queryXjProjRsts/${self.pageNo}/${self.pageSize}`,
                type: 'get',
                dataType: 'json',
                headers:{'IsMCompany':ISMCOMPANY},
                contentType: "application/json",
                success: function (resp) {
                    if (resp.success) {
                        console.log(resp);
                        resolve(resp);
                    }
                    else {
                        alert(resp.msgInf);
                    }
                },
                error: function (err) {
                    alert('系统错误！');
                }
            });
        });
    },
    // 渲染列表

    renderProjOnResultsList: function(resp){
        let self = ProjOnResultsList;
        // 处理数据begin
        self.total = resp.data.count;
        self.isFirstPage = self.pageNo === 1;
        self.maxPage = Math.ceil(self.total / self.pageSize);
        // 处理数据end
        if(self.pageNo * self.pageSize >= self.total){
            self.isLastPage = true;
        }else{
            self.isLastPage = false;
        }

        $.each(resp.data.list,function(index,item){
            if (item.rstPubTim!=null && item.rstPubTim !='') {
                item.rstPubTimFmt = (new Date(item.rstPubTim)).Format("yyyy-MM-dd");
            } else {
                item.rstPubTimFmt = '';
            }
            if (item.projSts=='-20') {
                item.projStsFmt="color-red";
                item.infoFmt="1";
            } else {
                item.infoFmt="2";
            }
        });

        let resultHtml = ProjOnResultsListTpl({ProjOnResultsList: resp.data.list});
        $('ul.categories').html(resultHtml);
    },

    // 渲染分页
    renderPagination: function(){
        let self = ProjOnResultsList;
        let pageList = [];
        for(let i=0; i<self.maxPage; i++){
            pageList.push({idx: i+1});
        }
        console.log(self.total);
        let paginationHtml = paginationTpl({
            begin: (self.pageNo-1) * self.pageSize + 1,
            end: self.isLastPage ? self.total : self.pageNo * self.pageSize,
            total: self.total,
            isLastPage: self.isLastPage,
            isFirstPage: self.isFirstPage,
            pageList: pageList

        });
        $('.J_pagination').html(paginationHtml);
        $('.page-item').each((index,item)=>{
            if($(item).data('index')===self.pageNo){
                console.log(this);
                $(item).addClass('active');
            }
            //最多显示7个页码
            if (Math.abs($(item).data('index')-self.pageNo) > 3) {
                $(item).css('display','none');
            }
        });

    },

    // 绑定事件
    bindEvent: function(){
        let self = ProjOnResultsList;
        $('.J_prev').unbind().click(function(){
            if(self.pageNo > 1){
                self.pageNo = self.pageNo -1;
                self.renderList();
            }
        });
        $('.J_next').unbind().click(function(){
            if(self.pageNo < self.maxPage){
                self.pageNo = self.pageNo + 1;
                self.renderList();
            }

        });

        //首尾页
        $('.isFirst').unbind().click(function(){
            if(self.pageNo != 1){
                self.pageNo = 1;
                self.renderList();
            }
        });
        $('.isLast').unbind().click(function(){
            if(self.pageNo != self.maxPage){
                self.pageNo = self.maxPage;
                self.renderList();
            }
        });

        $('.page-item').unbind().click(function(){
            let $item = $(this);

            if(!$item.hasClass('active')){
                self.pageNo = $item.data('index');
                self.renderList();
            }
        });
    },

    // 渲染列表
    renderList(){
        this.getProjOnResultsData()
            .then(this.renderProjOnResultsList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    }
};

ProjOnResultsList.init();